<?php

class _F_EXTENSION_I18N_ extends _F_EXTENSION_ {

	public static $_f_i18n_instance_;
	
	public $lang = 'en';
	public $text = null;
	
	public function __construct() {
		_F_EXTENSION_I18N_::$_f_i18n_instance_ = $this;
		f()->register_method('i18n',array($this,'i18n'));
	}
	
	public function i18n() {
		return $this;
	}

	public function text($text,$context = null) {
		if (!isset($this->text)) {
			$this->_f_load_resource_($this->lang);
		}
		if (isset($context)) {
			$ctext = "{{$context}}:$text";
			if (isset($this->text[$ctext])) return $this->text[$ctext];
		}
		if (isset($this->text[$text])) return $this->text[$text];
		return $text;
	}
	
	private function _f_load_resource_($lang) {
		$path = f()->base_path.'/i18n/'.$lang.'.php';
		$text = null;
		if (file_exists($path)) {
			include $path;
			$this->text = $text;
		}
	}
	
}

new _F_EXTENSION_I18N_;

function _f_i18n_text_($text,$context = null) {
	return _F_EXTENSION_I18N_::$_f_i18n_instance_->text($text,$context);
} 